package com.hp.attendance.dao;

import com.hp.attendance.entity.Attendance;
import com.hp.attendance.entity.dto.LeaveEmpDto;
import com.hp.attendance.entity.Leave;
import com.hp.attendance.entity.Role;
import com.hp.attendance.util.BaseDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class LeaveDao extends BaseDAO {
    /**
     * 请假记录查询
     *
     * @return
     */
    public List<Leave> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from lea";
        List<Leave> list = queryList(sql, new Object[0], new Leave());
        return list;
    }

    //根据status状态查询返回list
    public List<Leave> getListByStatus(String status) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from lea where status=?";
        Object[] obj = {status};
        List<Leave> list = queryList(sql, obj, new Leave());
        return list;
    }

    //根据
    public Leave getLeaveById(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from lea where id=?";
        Object[] obj = {id};
        Leave leave = queryOne(sql, obj, new Leave());
        return leave;
    }

    public void leaveUpdateById(String status, int id) throws SQLException, IOException, ClassNotFoundException {
        String sql = "update lea set status=? where id=?";
        Object[] obj = {status, id};
        addOrUpdOrDel(sql, obj);
    }

    public List<Leave> getListemployee_number(int employee_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from lea where employee_number=?";
        Object[] obj = {employee_number};
        List<Leave> list = queryList(sql, obj, new Leave());
        return list;
    }

    /**
     * 分页查询记录
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public List<Leave> getLimitList(int employee_number, int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from lea where employee_number=? limit ?,?";
        Object[] obj ={employee_number,(pageNo-1)*pageSize,pageSize};
        List<Leave> list = queryList(sql, obj, new Leave());
        return list;
    }

    /**
     * 查询总记录数
     * @return
     * @throws SQLException
     */
    public long getTotal(int employee_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from lea where employee_number=?";
        Object[] obj ={employee_number};
        int count = queryCount(sql, obj);
        return count;
    }

    //添加申请请假记录
    public void addLeave(Leave leave) throws SQLException, IOException, ClassNotFoundException {
        String sql ="insert into lea values(null,?,?,?,?,?,?,?,null,'未批准',null)";
        Object[] obj ={leave.getEmployee_number(),leave.getDepartment_number(),leave.getStart_time(),leave.getEnd_time(),leave.getDays(),leave.getReason(),leave.getType()};
        addOrUpdOrDel(sql,obj);
    }
}
